using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FzWeb.Models;
using FzWeb.Filter;

namespace FzWeb.Controllers
{
    [ExceptionFilter]
    public class WorkExperienceController : Controller
    {
        private BaseDataService bds = new BaseDataService();
        public ActionResult Index()
        {
            TempData["maxOrder"] = bds.GetWorkExperienceList().Last().Order + 1;
            return View(bds.GetWorkExperienceList());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(WorkExperience we)
        {
            we.Order = (int)TempData["maxOrder"];
            bds.CreateWorkExperience(we);
            return RedirectToAction("Index");

        }


        public ActionResult Edit(int id)
        {
            return View(bds.GetWorkExperience(c => c.Id == id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(WorkExperience we)
        {
            bds.UpdateWorkExperience(we);
            return RedirectToAction("Index");
           
        }
        public ActionResult Delete(int id)
        {

            bds.DeleteWorkExperience(bds.GetWorkExperience(c => c.Id == id));
            return RedirectToAction("Index");

        }

        public ActionResult Up(int id)
        {
            WorkExperience e = bds.GetWorkExperience(c => c.Id == id);
            if (e.Order > 1)
            {
                WorkExperience before = bds.GetBeforeWorkExperience(e.Order);
                int temp = e.Order;
                e.Order = before.Order;
                before.Order = temp;
                bds.UpdateWorkExperience(e);
                bds.UpdateWorkExperience(before);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Down(int id)
        {
            WorkExperience e = bds.GetWorkExperience(c => c.Id == id);
            if (e.Order < (int)TempData["maxOrder"] - 1)
            {
                WorkExperience after = bds.GetAfterWorkExperience(e.Order);
                int temp = e.Order;
                e.Order = after.Order;
                after.Order = temp;
                bds.UpdateWorkExperience(e);
                bds.UpdateWorkExperience(after);
            }
            return RedirectToAction("Index");
        }
    }
}
